package com.woniuxy.service.impl;

import com.woniuxy.dao.AddressDao;
import com.woniuxy.dao.CategoryDao;
import com.woniuxy.pojo.Address;
import com.woniuxy.pojo.Category;
import com.woniuxy.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 罗蔚
 * @Date 2022/3/28 0:42
 * @Description TODO
 */
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;


    @Override
    public void addAddress(Address address) {
        if ("1".equals(address.getType())) {
            //把其他地址type改为"0"
            addressDao.updateAllTypeByUid(address.getUserid());
        }else {
            //如果是空, 就赋值为"0"
            address.setType("0");
        }

        if(address.getId() == 0) {
            //库里新建一个id,
            addressDao.addAddress(address);
        }else {
            //id不等于0, 就是修改,
            addressDao.updateByUidAndId(address);
        }
    }

    @Override
    public List<Address> findAllAddress(int uid) {
        return addressDao.findAllAddress(uid);
    }

    @Override
    public void deleteById(int id, int uid) {
        addressDao.deleteById(id, uid);
    }

    @Override
    public void setDefault(int id, int uid) {
        //根据uid将所有地址type改为"0"
        addressDao.updateAllTypeByUid(uid);

        //在将传下来的修改为"1"
        addressDao.updateTypeByUidAndId(id,uid);
    }

    @Override
    public Address findByIdAndUid(int id, int uid) {
        return addressDao.findByIdAndUid(id, uid);
    }

    @Override
    public List<Address> findFourAddress(int uid) {
        return addressDao.findFourAddress(uid);
    }
}
